<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class plugin_ineo
{
	/**
	* Global ipsclass
	* @var	object
	*/
	var $ipsclass;
	
	/**
	* Module type
	* @var	string
	*/
	var $module = 'ineo';
	
	/*-------------------------------------------------------------------------*/
	// show_attachment_get_sql_data
	/*-------------------------------------------------------------------------*/
	/**
	* Checks the attachment and checks for download / show perms
	* Returns TRUE or FALSE
	* IT really does
	*
	* @return boolean
	*/
	function show_attachment_get_sql_data( $attach_id )
	{
		if( $this->ipsclass->member['g_ineo_view'] )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// render_attachment_get_sql_data
	/*-------------------------------------------------------------------------*/
	/**
	* Check the attachment and make sure its OK to display
	* Returns TRUE or FALSE
	* IT really does
	*
	* @return boolean
	*/
	function render_attachment_get_sql_data( $attach_ids, $rel_ids=array(), $attach_post_key=0 )
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$rows  = array();
		$query = '';
		$match = 0;
		
		//-----------------------------------------
		// Check
		//-----------------------------------------
		
		if ( ! is_array( $attach_ids ) OR ! count( $attach_ids ) )
		{
			$attach_ids = array( -2 );
		}
		
		if ( $attach_post_key )
		{
			$query .= " OR attach_post_key='".$this->ipsclass->DB->add_slashes( $attach_post_key )."'";
			$match  = 2;
		}
				
		//-----------------------------------------
		// Grab 'em
		//-----------------------------------------
		
		$this->ipsclass->DB->build_query( array( 'select'   => '*',
												 'from'     => 'attachments',
												 'where'    => "attach_rel_module='".$this->module."' AND ( attach_id IN(-1,". implode( ",", $attach_ids ) . ") " . $query . " )",
										)      );

		$attach_sql = $this->ipsclass->DB->exec_query();
		
		
		//die( $this->ipsclass->DB->get_num_rows() );
		//-----------------------------------------
		// Loop through and filter off naughty ids
		//-----------------------------------------
		
		while( $db_row = $this->ipsclass->DB->fetch_row( $attach_sql ) )
		{
			$_ok = 1;
			
			if ( $match == 1 )
			{
				if ( ! in_array( $db_row['attach_rel_id'], $rel_ids ) )
				{
					$_ok = 0;
				}
			}
			else if ( $match == 2 )
			{
				if ( $db_row['attach_post_key'] != $attach_post_key )
				{
					$_ok = 0;
				}
			}
			
			//-----------------------------------------
			// Ok?
			//-----------------------------------------
			
			if ( $_ok )
			{
				$rows[ $db_row['attach_id'] ] = $db_row;
			}
		}
		
		//-----------------------------------------
		// Return
		//-----------------------------------------
	
		return $rows;
	}
	
	/*-------------------------------------------------------------------------*/
	// Remove attachment clean up
	/*-------------------------------------------------------------------------*/
	/**
	* Recounts number of attachments for the articles row
	*
	* @return boolean
	*/
	function post_process_upload( $post_key, $rel_id, $args=array() )
	{
		return true;
	}
	
	/*-------------------------------------------------------------------------*/
	// Remove attachment clean up
	/*-------------------------------------------------------------------------*/
	/**
	* Recounts number of attachments for the articles row
	*
	* @return boolean
	*/
	function remove_attachment_clean_up( $attachment )
	{
		return true;
	}
	
	/*-------------------------------------------------------------------------*/
	// Bulk remove attachment permissions check
	/*-------------------------------------------------------------------------*/
	/**
	* Returns TRUE or FALSE
	* IT really does
	*
	* @return boolean
	*/
	function return_bulk_remove_permission( $attach_rel_ids=array() )
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$ok_to_remove = FALSE;
		
		//-----------------------------------------
		// Allowed to remove?
		//-----------------------------------------
		
		if ( $this->ipsclass->member['group_access_cp'] )
		{
			$ok_to_remove = TRUE;
		}
		
		return $ok_to_remove;
	}
	
	/*-------------------------------------------------------------------------*/
	// Remove attachment permissions check
	/*-------------------------------------------------------------------------*/
	/**
	* Returns TRUE or FALSE
	* IT really does
	*
	* @return boolean
	*/
	function return_remove_permission( $attachment )
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$ok_to_remove = FALSE;
		
		//-----------------------------------------
		// Check
		//-----------------------------------------
		
		if ( ! $attachment['attach_id'] )
		{
			return FALSE;
		}
		
		//-----------------------------------------
		// Allowed to remove?
		//-----------------------------------------
		
		if ( $this->ipsclass->member['id'] == $attachment['attach_member_id'] )
		{
			$ok_to_remove = TRUE;
		}
		else if ( $this->ipsclass->member['g_ineo_ismod'] )
		{
			$ok_to_remove = TRUE;
		}
				
		return $ok_to_remove;
	}
	
	/*-------------------------------------------------------------------------*/
	// Get allowed upload sizes
	/*-------------------------------------------------------------------------*/
	/**
	* get_space_allowance
	*
	* Returns an array of the allowed upload sizes in bytes.
	* Return 'space_allowed' as -1 to not allow uploads.
    * Return 'space_allowed' as 0 to allow unlimited uploads
    * Return 'max_single_upload' as 0 to not set a limit
	*
	* @param	id	    Member ID
	* @param	string  MD5 post key
	* @return	array [ 'space_used', 'space_left', 'space_allowed', 'max_single_upload' ]
	*/
	function get_space_allowance( $post_key='', $member_id='' )
	{
		$max_php_size      = intval( $this->ipsclass->math_get_post_max_size() );
		$member_id         = intval( $member_id ? $member_id : $this->ipsclass->member['id'] );
		$forum_id          = intval( $this->ipsclass->input['forum_id'] ? $this->ipsclass->input['forum_id'] : $this->ipsclass->input['f'] );
		$space_left        = 0;
		$space_used        = 0;
		$space_allowed     = 0;
		$max_single_upload = 0;
		$space_calculated  = 0;
		
		if( !$this->ipsclass->member['g_ineo_upload'] )
		{
			return array('space_allowed' => -1, 'max_single_upload' => -1);
		}
		
		// Generate total space allowed
		$total_space_allowed = $this->ipsclass->member['g_attach_max'] * 1024;
		
		if ( ! $space_calculated )
		{
			// Generate space allowed figure
			
			if ( $this->ipsclass->member['g_attach_max'] > 0 )
			{
					//-----------------------------------------
					// Global limit...
					//-----------------------------------------
					
					$_space_used = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'SUM(attach_filesize) as figure',
																					 'from'   => 'attachments',
																					 'where'  => 'attach_member_id='.$member_id ) );

					$space_used    = intval( $_space_used['figure'] );
					$space_allowed = intval( ( $this->ipsclass->member['g_attach_max'] * 1024 ) - $space_used );
					$space_allowed = $space_allowed < 0 ? 0 : $space_allowed;
			}
			else
			{
				# Unlimited
				$space_allowed = 0;
			}
			
			//-----------------------------------------
			// Generate space left figure
			//-----------------------------------------
			
			$space_left = $space_allowed ? $space_allowed : 0;
			$space_left = ($space_left < 0) ? 0 : $space_left;
			
			//-----------------------------------------
			// Generate max upload size
			//-----------------------------------------
			
			if ( ! $max_single_upload )
			{
				if ( $space_left > 0 AND $space_left < $max_php_size )
				{
					$max_single_upload = $space_left;
				}
				else if ( $max_php_size )
				{
					$max_single_upload = $max_php_size;
				}
			}
		}
		
		$return = array( 'space_used' => $space_used, 'space_left' => $space_left, 'space_allowed' => $space_allowed, 'max_single_upload' => $max_single_upload, 'total_space_allowed' => $total_space_allowed );
		
		return $return;
	}
	
	/*-------------------------------------------------------------------------*/
	// Get settings
	/*-------------------------------------------------------------------------*/
	/**
	* get_settings
	*
	* Returns an array of settings:
	* 'siu_thumb'	= Allow thumbnail creation?
	* 'siu_height'  = Height of the generated thumbnail in pixels
	* 'siu_width'   = Width of the generated thumbnail in pixels
	* 'upload_dir'  = Base upload directory (must be a full path)
	*
	* You can omit any of these settings and IPB will use the default
	* settings (which are the ones entered into the ACP for post thumbnails)
	*
	* @return	boolean
	*/
	function get_settings()
	{
		$this->settings = array();
		
		return true;
	}

}

?>